﻿
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using G.Runtime;
using GameDesigner.Source.Widgets;

namespace GameRuntimeControls
{
    public partial class PlayerStatusControl : UserControl
    {
        private readonly List<BoundVariableWidget> _BoundVariableWidgets = new List<BoundVariableWidget>();
        
        private LabelWidget _Label;
        private HasGameSpace _HasGame;
        private string _PlayerName;
        
        public PlayerStatusControl()
        {
            //
            // The InitializeComponent() call is required for Windows Forms designer support.
            //
            InitializeComponent();
            
        }
        
        public HasGameSpace Game
        {
            get { return _HasGame; }
            set
            {
                if (_HasGame != value)
                {
                    _HasGame = value;
                    SetupDisplay();
                }
            }
        }

        public string PlayerName
        {
            get { return _PlayerName; }
            set
            {
                if (_PlayerName != value)
                {
                    _PlayerName = value;
                    SetupDisplay();
                }
            }
        }
        
        void SetupDisplay()
        {
            if (_HasGame == null || _PlayerName == null)
            {
                return;
            }
            
            if (!_HasGame.Game.Players.Contains(_PlayerName)) 
            {
                return;
            }
            
            if (_Label != null)
            {
                _Label.Dispose();
            }
            foreach (BoundVariableWidget widget in _BoundVariableWidgets) 
            {
                widget.Dispose();
            }
            
            GamePlayer player = _HasGame.Game.Players[_PlayerName];
            base.BackColor = player.Color;
            
            Point point = new Point(10, 10);
            _Label = new LabelWidget(this, player.Name, point);
            
            foreach (GameValue value in player.GameValues) 
            {
                point = new Point(point.X, point.Y + 20);
                _BoundVariableWidgets.Add(
                    new BoundVariableWidget(value, this, point));
            }
        }
    }
}
